package com.cykj.pos.util.epse;

import com.alibaba.fastjson.JSONObject;
import com.cykj.pos.domain.epsp.Response;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;


public class RemoteInvoker {
    private final static DateFormat df = new SimpleDateFormat("yyyyMMddHHmmss");

	public static <T extends Response> T invoke(Object request, String url, Class<T> c) throws Exception {
		final String jsonData = request instanceof String? (String)request: JSONObject.toJSONString(request);
//        final String sign = RsaUtils.sign(KeyUtil.getPrimaryKey(), jsonData);//签名
        final String sign = RsaUtils.sign("MIIEvQIBADANBgkqhkiG9w0BAQEFAASCBKcwggSjAgEAAoIBAQCfu9UpJpxH6J4qul/j/oqVK9zxjPTAUcbp6m3HLJFRWfyTbIcRJewcohfO0VrwiLP65UOaMa6EEgPO3MNvJYvEphZwTzjO73IoWOMF2MnCg7wECVdnbJ4IOAebdZx7dAJvuruD++qilCk3MSMmkjy4dVU4Cs73ksPKU/effVMOhrsvc/nIlpERmfC0IPHcU6aUCM4ikx16yKmp8smE1uHxs94K2TdQdr0M7KkRMprbWzfKhydu6ESP1QFNnzzJxo5t8TnLOtCF2oYPh3uPvTmT2nTBNpmHSVSYoqaeEyZExnRzZhjDG3QB0K3aibulp/7jxBPI5uufb1yDxP5jOavfAgMBAAECggEAe43p+xOvjPvE5F4XcB7jCTHNjFYwt1ZKG4PG+Gpj2V5WvZ4T99KyiQ9khQhxZF8JgIe4O9w0Flps8aTX0vBi5XBnKOZOldqnHchriok+vyPw2dpz7j8ZGlhkof5eVSvN+qihBR1mR+182AE9JEe3iiLQETJjFyNCKQ28G3TwOuoJd3+ahNNOJ8Btq6E3Lst28pm+vDpyPvro3hQyi6wspkPhwYAKFBY9BuyyNIOBkY09hErS8Fb/Iw0mJN1bBmW+I+CL8T9DuFH8hbLdottafIYlgjyUA/kTf8IlVzcbC5wwJ843WokVZl0JTJD3syjTWLdF4DSX+58+z/HekhqPMQKBgQDRaBo0lPn8bW7g8L4JM6nsHkZD43TVGPKjBLus6ImmbCoisBJ1EY1SoUxIJmsmgiRL+GYkp68Di050mkHfwamfTxx+4fb7URoxBJ+gEyg8AhhMhX21jH2g9uthX9YGQ46xvi2h/Kc8onzI3nAIPviPUkNApgkRqJiCdRTX8mPVhwKBgQDDRlZ4+szJwH2h0blSCEBWBSQqQZOke2dHsxo7kA1B5HxNxHvzOaIHOjQV6IXyBIMBFW29TzsTGk9TqSasIZYgYASeyKAwAd0l+RuFtWjm91YL5AX7kMKf21FtLoqFJdGvm9o9eVpySd43QvDHe/5hSMKB2AShJT9nqeWH+1YM6QKBgDqlPwGO31v0geL5SR9mmqYH7IHCx8uOSxWgk7bfAP3cuQgx61lh6wRDLAvgxo1oUSX9kQ2e8IK7luNM+DnkEM8O5toaCoNjuUQVGWdJNjFoHMoBcK1RhTV5MX55yoTCksnwivMcwPFYSD4xHYsRp6br0O3gEhYh82RFyeqYdaEtAoGAUNGnsGJ8p3OXi4W1YZIwrmlqUmLZrFW2pSInVUeFdSQPnlAXzM6plLwuGnMmHMhpP+WSiatlE77r3gBAFTXBYJzczKrUQYpD8RdsoKY4SH9NiZZSM4/yPR8UUE/W3MyOQFQMuyyilnKkGdMGvElBOxpWBk9VeyxjVZAng2QudSECgYEAnylInHXx9WxqQ/7cFkhDbfsNwfP4C2QU+FOfvIOg7Tke7E29inCWikWJe7nAs8l55zDMArOoMHywkfAz6+cUFIoh0Y7ksXxemEBII8zDcj+XAzgL5AUC9R6oX8NYMOrURHuolyalrn1YzGuT8ReOz+MTc0gU/P0++qqzJhFFIBs=", jsonData);//签名

        Map<String , String> header = new HashMap<String , String>();
        header.put("x-efps-sign", sign);
        header.put("x-efps-sign-no", Config.getSignNo());
        header.put("x-efps-sign-type","SHA256withRSA");
        header.put("x-efps-timestamp",df.format(new Date()));
        header.put("Content-Type", "application/json");
        T response = HttpUtil.post(url, jsonData ,header , c);

        return response;
	}
}
